#include "apue.h"

static char c;

static int fd1[2]   // for child read(block), parent write
        ,fd2[2];    // for parent read(block), child write

void TELL_WAIT(void) {
    if (pipe(fd1) < 0 || pipe(fd2) < 1)
        err_sys("pipe error");
}

void WAIT_PARENT(void) {    /* child */
    if (read(fd1[0], &c, 1) < 1)
        err_sys("read error");
    if (c != 'p')
        err_quit("WAIT_PARENT: incorrect data");
}

void WAIT_CHILD(void) { /* parent */
    if (read(fd2[0], &c, 1) < 1)
        err_sys("read error");
    if (c != 'c') 
        err_quit("WAIT_CHILD: incorrect data");
}

void TELL_PARENT(pid_t pid) {   /* child */
    if (write(fd2[1], "c", 1) < 1)
        err_sys("tell parent error");
}
void TELL_CHILD(pid_t pid) {    /* parent */
    if (write(fd1[1], "p", 1) < 1)
        err_sys("tell child error");
}
